<template>
  <div>
    <!-- 导航条区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容"
                    v-model="goods.query"
                    clearable
                    @clear="getGoodsList">
            <el-button slot="append"
                       icon="el-icon-search"
                       @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary"
                     @click="goAddgoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表区域 -->
      <el-table :data="goodsList"
                style="width: 100%"
                border
                stripe>
        <el-table-column type="index">
        </el-table-column>
        <el-table-column prop="goods_name"
                         label="商品名称"
                         width="300">
        </el-table-column>
        <el-table-column prop="goods_price"
                         label="商品价格（元）"
                         width="80">
        </el-table-column>
        <el-table-column prop="goods_weight"
                         label="商品重量"
                         width="80">
        </el-table-column>
        <el-table-column prop="add_time"
                         label="添加时间"
                         width="180">
          <template slot-scope="scope">
            {{scope.row.add_time|dataFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary"
                       size="mini"
                       icon="el-icon-edit">编辑</el-button>
            <el-button type="danger"
                       size="mini"
                       icon="el-icon-delete"
                       @click="removeGoods(scope.row.goods_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="goods.pagenum"
                     :page-sizes="[2, 5, 10, 20]"
                     :page-size="goods.pagesize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data () {
    return {
      // 获取商品列表参数
      goods: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      goodsList: [],
      total: 0
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    getGoodsList: function () {
      const that = this
      this.$http.get('goods', {
        params: this.goods
      }).then(
        function (res) {
          if (res.data.meta.status !== 200) {
            that.$message.error('获取商品列表失败')
          }
          that.goodsList = res.data.data.goods
          that.total = res.data.data.total
        })
    },
    // 修改每页显示数量
    handleSizeChange: function (newSize) {
      console.log(newSize)
      this.goods.pagesize = newSize
      this.getGoodsList()
    },
    // 修改当前页
    handleCurrentChange: function (newPage) {
      this.goods.pagenum = newPage
      this.getGoodsList()
    },
    // 删除商品
    removeGoods (id) {
      const that = this
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete(`goods/${id}`).then(
          function (res) {
            if (res.data.meta.status !== 200) {
              that.$message.error('删除商品失败')
            }
            that.getGoodsList()
          })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 跳转到添加商品页面
    goAddgoods: function () {
      this.$router.push('/goods/add')
    }
  }
}
</script>
<style lang="less" scoped>
.el-card {
  margin-top: 10px;
  line-height: 0;
}
.el-row {
  margin-bottom: 10px;
}
</style>
